package com.ruoyi.web.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.web.dto.HazardManagementDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import com.ruoyi.web.entity.HazardManagement;
import com.ruoyi.web.service.IHazardManagementService;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lqc
 * @since 2024-01-30
 */
@RestController
@RequestMapping("/admin/management")
public class HazardManagementController extends BaseController {

    private final IHazardManagementService hazardManagementService;
    public HazardManagementController(IHazardManagementService hazardManagementServiceImpl){
        this.hazardManagementService = hazardManagementServiceImpl;
    }
    /**
     * 查询隐患处理列表
     */
    @PreAuthorize("@ss.hasPermi('admin:management:list')")
    @GetMapping("/list")
    public TableDataInfo list(HazardManagement hazardManagement)
    {
        startPage();
        List<HazardManagementDto> list = hazardManagementService.selectHazardManagementList(hazardManagement);
        return getDataTable(list);
    }

    /**
     * 导出隐患处理列表
     */
    @PreAuthorize("@ss.hasPermi('admin:management:export')")
    @Log(title = "隐患处理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HazardManagement hazardManagement) {
        List<HazardManagementDto> hazardManagementDtos = hazardManagementService.selectHazardManagementList(hazardManagement);
        List<HazardManagement> hazardManagements = new ArrayList<>();

        for (HazardManagementDto hazardManagementDto : hazardManagementDtos) {
            HazardManagement temp = new HazardManagement(); // 在循环内创建新实例
            BeanUtil.copyProperties(temp, hazardManagementDto); // 将DTO的属性复制到新的隐患管理实例中
            hazardManagements.add(temp); // 将新创建的对象添加到列表中
        }
        ExcelUtil<HazardManagement> util = new ExcelUtil<HazardManagement>(HazardManagement.class);
        util.exportExcel(response, hazardManagements, "隐患处理数据"); // 导出整个列表
    }

    /**
     * 获取隐患处理详细信息
     */
    @PreAuthorize("@ss.hasPermi('admin:management:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(hazardManagementService.selectHazardManagementById(id));
    }

    /**
     * 新增隐患处理
     */
    @PreAuthorize("@ss.hasPermi('admin:management:add')")
    @Log(title = "隐患处理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HazardManagement hazardManagement) {
        hazardManagement.setId(IdUtil.getSnowflakeNextId());
        return toAjax(hazardManagementService.insertHazardManagement(hazardManagement));
    }

    /**
     * 修改隐患处理
     */
    @PreAuthorize("@ss.hasPermi('admin:management:edit')")
    @Log(title = "隐患处理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HazardManagement hazardManagement)
    {
        return toAjax(hazardManagementService.updateHazardManagement(hazardManagement));
    }

    /**
     * 删除隐患处理
     */
    @PreAuthorize("@ss.hasPermi('admin:management:remove')")
    @Log(title = "隐患处理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(hazardManagementService.deleteHazardManagementByIds(ids));
    }
}
